package com.example.module_word.dialog

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.module_word.R
import com.example.module_word.adapter.DialogLanguageAdapter
import com.example.module_word.`interface`.DialogListener
import com.fenghuajueli.data.scanning_spokenLanguage
import com.fenghuajueli.data.translation_character
import com.fenghuajueli.`interface`.LanguageClickListener
import com.jin_mo.custom.dialog.PrivacyDialog
import com.ll.module_translate.utils.Scanning
import com.ll.module_translate.utils.TransLanguage


/**
 * @ProjectName: ModuleDev
 * @Package: com.fenghuajueli.dialog
 * @Description:
 * @Author: sz_ijj
 * @CreateDate: 2022/3/3 11:41
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/3/3
 * @UpdateRemark: 更新说明
 */

private val dialogAdapter by lazy {
    DialogLanguageAdapter()
}

private var mDialogListener: DialogListener? = null

fun setDiaLogDissmiss(dialogListener: DialogListener) {
    mDialogListener = dialogListener
}

fun showTranslationDialog(context: Context,mFrom: Boolean) {
    dialogAdapter.refreshData(translation_character)
    val dialog = PrivacyDialog(
        context, R.layout.dialog_select_launch,
        true,
        Gravity.BOTTOM,
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT
    )
    val tvFrom = dialog.findViewById<TextView>(R.id.tv_dialog_from)
    val tvTo = dialog.findViewById<TextView>(R.id.tv_dialog_to)
    val ivQie = dialog.findViewById<ImageView>(R.id.iv_dialog_qie)

    tvFrom.isSelected = mFrom
    tvTo.isSelected = !mFrom


    tvFrom.text = TransLanguage.CToRCN().first
    tvTo.text = TransLanguage.CToRCN().second

    //弹窗隐藏按钮
    dialog.findViewById<ImageView>(R.id.iv_dialog_select_language_down).setOnClickListener {
        dialog.dismiss()
    }

    dialog.findViewById<View>(R.id.view_dialog_dissmiss).setOnClickListener {
        dialog.dismiss()
    }

    with(dialog.findViewById<RecyclerView>(R.id.rv_dialog_select_language)) {
        layoutManager = LinearLayoutManager(this.context)
        adapter = dialogAdapter
    }

    ivQie.setOnClickListener {
        val fromCN = TransLanguage.CToRCN().first
        val toCN = TransLanguage.CToRCN().second

        val from = TransLanguage.CToR().first
        val to = TransLanguage.CToR().second

        if (fromCN == "自动检测") {
            return@setOnClickListener
        }

        TransLanguage.saveFrom(toCN, to)
        TransLanguage.saveTo(fromCN, from)

        tvFrom.text = TransLanguage.CToRCN().first
        tvTo.text = TransLanguage.CToRCN().second
        dialog.dismiss()
    }

    dialogAdapter.setClickListener(object : LanguageClickListener {
        override fun click(chinese: String, en: String) {
            if (tvFrom.isSelected) {
                if (TransLanguage.CToRCN().second == chinese) {
                    if (chinese == "中文(简体)") {
                        TransLanguage.saveTo("英语", "en")
                        tvTo.text = TransLanguage.CToRCN().second
                    } else {
                        TransLanguage.saveTo("中文(简体)", "zh")
                        tvTo.text = TransLanguage.CToRCN().second
                    }
                }
                TransLanguage.saveFrom(chinese, en)
            } else {
                if (TransLanguage.CToRCN().first == chinese || chinese.trim() == "自动检测") {
                    ToastUtils.showShort("不可以选择此语言")
                    return
                }
                TransLanguage.saveTo(chinese, en)
            }

            tvFrom.text = TransLanguage.CToRCN().first
            tvTo.text = TransLanguage.CToRCN().second
            dialog.dismiss()
            mDialogListener!!.dismissDialog()
        }
    })

    tvFrom.setOnClickListener {
        tvFrom.isSelected = true
        tvTo.isSelected = false
    }

    tvTo.setOnClickListener {
        tvFrom.isSelected = false
        tvTo.isSelected = true
    }

}

fun pictureTranslationDialog(context: Context,mFrom: Boolean) {
    dialogAdapter.refreshData(scanning_spokenLanguage)

    val dialog = PrivacyDialog(context, R.layout.dialog_select_launch, true, Gravity.BOTTOM, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)

    val tvFrom = dialog.findViewById<TextView>(R.id.tv_dialog_from)
    val tvTo = dialog.findViewById<TextView>(R.id.tv_dialog_to)
    val ivQie = dialog.findViewById<ImageView>(R.id.iv_dialog_qie)


    tvFrom.isSelected = mFrom
    tvTo.isSelected = !mFrom

    tvFrom.text = Scanning.ScanFromToEnCn().first
    tvTo.text = Scanning.ScanFromToEnCn().second

    //弹窗隐藏按钮
    dialog.findViewById<ImageView>(R.id.iv_dialog_select_language_down).setOnClickListener {
        dialog.dismiss()
    }

    dialog.findViewById<View>(R.id.view_dialog_dissmiss).setOnClickListener {
        dialog.dismiss()
    }

    with(dialog.findViewById<RecyclerView>(R.id.rv_dialog_select_language)) {
        layoutManager = LinearLayoutManager(this.context)
        adapter = dialogAdapter
    }

    ivQie.setOnClickListener {
        val fromCN = Scanning.ScanFromToEnCn().first
        val toCN = Scanning.ScanFromToEnCn().second

        val from = Scanning.ScanFromToEn().first
        val to = Scanning.ScanFromToEn().second

        if (fromCN == "自动检测") {
            return@setOnClickListener
        }

        Scanning.saveFrom(toCN, to)
        Scanning.saveTo(fromCN, from)

        tvFrom.text = Scanning.ScanFromToEnCn().first
        tvTo.text = Scanning.ScanFromToEnCn().second
        dialog.dismiss()
    }

    dialogAdapter.setClickListener(object : LanguageClickListener {
        override fun click(chinese: String, en: String) {
            LogUtils.d(chinese, en)
            if (tvFrom.isSelected) {
                if (Scanning.ScanFromToEnCn().second == chinese) {
                    if (chinese == "中文") {
                        Scanning.saveTo("英语", "en")
                    } else {
                        Scanning.saveTo("中文", "zh")
                    }
                }
                Scanning.saveFrom(chinese, en)
            } else {
                if (Scanning.ScanFromToEnCn().first == chinese || chinese.trim() == "自动检测") {
                    ToastUtils.showShort("不可以选择此语言")
                    return
                }
                Scanning.saveTo(chinese, en)
            }

            tvFrom.text = Scanning.ScanFromToEnCn().first
            tvTo.text = Scanning.ScanFromToEnCn().second
            dialog.dismiss()
            mDialogListener!!.dismissDialog()
        }
    })

    tvFrom.setOnClickListener {
        tvFrom.isSelected = true
        tvTo.isSelected = false
    }

    tvTo.setOnClickListener {
        tvFrom.isSelected = false
        tvTo.isSelected = true
    }


}